package com.zhida.smartcontrol.common.mapsel;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.jay.tools.ui.TopBarView;
import com.jay.tools.utils.DimenUtils;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/11/23 023 10:08
 * description: 选择经纬度
 */
public class MapSelFragment extends MVPBaseFragment<MapSelContract.View, MapSelPresenter> implements MapSelContract.View {
    @BindView(R.id.top)
    TopBarView mTopBar;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_wd)
    TextView mTvWd;
    @BindView(R.id.tv_jd)
    TextView mTvJd;
    @BindView(R.id.bmapView)
    MapView mMapView;

    private String mLat, mLng;
    private BaiduMap mBaiduMap;

    public static MapSelFragment newInstance() {
        Bundle args = new Bundle();
        MapSelFragment fragment = new MapSelFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_map_sel;
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        inflateTopBar(mTopBar);
        mBaiduMap = mMapView.getMap();
        mPresenter.initBaiduMap(mBaiduMap);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        stateBar(true);
        if (null != mMapView) {
            mMapView.onResume();
        }
        mPresenter.startLocation();
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        stateBar(false);
        if (null != mMapView) {
            mMapView.onPause();
        }
        mPresenter.stopLocation();
    }

    @Override
    public void onDestroy() {
        mPresenter.setMyLocationEnabled(false);
        mPresenter.stopLocation();
        if (null != mMapView) {
            mMapView.onDestroy();
            mMapView = null;
        }
        super.onDestroy();
    }

    @OnClick({R.id.tv_search, R.id.btn_ok})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_search:
                break;
            case R.id.btn_ok:
                Bundle bundle = new Bundle();
                bundle.putString("lat", mLat);
                bundle.putString("lng", mLng);
                setFragmentResult(0, bundle);
                pop();
                break;
        }
    }

    @Override
    protected void initLazyEvents() {
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                mBaiduMap.clear();

                final CircleOptions ooCircle = new CircleOptions().fillColor(0x303862FF)
                        .center(latLng)
                        .radius(DimenUtils.dp2px(getProxyActivity(), 96));
                mBaiduMap.addOverlay(ooCircle);

                BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ic_location_dot_blue);

                OverlayOptions option = new MarkerOptions().position(latLng).icon(bitmap);
                mBaiduMap.addOverlay(option);
                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latLng));

                mPresenter.findAddress(latLng.latitude, latLng.longitude);
            }

            @Override
            public void onMapPoiClick(MapPoi mapPoi) {

            }
        });
    }

    @Override
    public void showLatLng(BaiduAddressBean.ResultBean result) {
        mTvTitle.setText(result.getFormatted_address() + "附近");
        BaiduAddressBean.ResultBean.LocationBean location = result.getLocation();
        DecimalFormat df = new DecimalFormat("######0.000000");

        mLat = df.format(location.getLat()) + "";
        mLng = df.format(location.getLng()) + "";
        mTvWd.setText("纬度：" + mLat);
        mTvJd.setText("经度" + mLng);
        mPresenter.stopLocation();
    }

}
